package project.active;


import java.util.ArrayList;
import java.util.List;

import model.pojo.Employee;

import project.adapter.Projectdetail_error_adapter_tc;
import project.dao.pr_projectDAO;
import project.model.PrComment;
import project.model.PrProject;
import project.model.PrTestProject;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
import approve.action.R;

public class Projectdetail_error_tc extends Activity {
	
	EditText comment=null;
	TextView title_content,contenterror_content,logout,username,report,lbtitle,lbcontenterror,lbcomment,lblistcommnet;
	Button btok,btcancel;
	RadioButton rdnofix,rderror,rdfixcomplete;

	String namecolor="#B023AB";//mau tim
	PrProject pro=new PrProject();
	PrTestProject err=new PrTestProject();
	pr_projectDAO prdao=new pr_projectDAO();
	PrComment prcom=new PrComment();
	ListView addNewError,listcomment;
	Employee emp=new Employee();
	String lang = "EN",database="admin";
	
	Bundle language;
	
	public String getLang() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			lang = extras.getString("lang");
		}
		return lang;
	}
	public String getDatabase() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			database = extras.getString("database");
		}
		return database;
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.projectdetail_error_tc);
		
		language=getIntent().getExtras().getBundle(getLang());
		
		Intent i=this.getIntent();
		pro=(PrProject)i.getSerializableExtra("project");
		err=(PrTestProject)i.getSerializableExtra("errorproject");
		emp=(Employee)i.getSerializableExtra("em");

		comment=(EditText)findViewById(R.id.txtcontent);
		title_content=(TextView)findViewById(R.id.title_content);
		contenterror_content=(TextView)findViewById(R.id.contenterror_content);
		listcomment=(ListView)findViewById(R.id.listcomment);
		
		btok=(Button)findViewById(R.id.btok);
		btcancel=(Button)findViewById(R.id.btcancel);
		
		addNewError=(ListView)findViewById(R.id.listerror);
		if(err.getStatus()==0)
			btok.setOnClickListener(clickListenerOK);
		btcancel.setOnClickListener(clickListenerCancel);
		
		title_content.setText(err.getTitleview());
		contenterror_content.setText(err.getText());
		Projectdetail_error_adapter_tc adapter1 = new Projectdetail_error_adapter_tc(this, loadAllComment());
		listcomment.setAdapter(adapter1);
		listcomment.setOnItemClickListener(showMenu);
		
		logout=(TextView)findViewById(R.id.logout);
		logout.setText(language.getString("Logout"));
		username=(TextView)findViewById(R.id.username);
		
		logout.setOnClickListener(listenerLogout);
		username.setText(emp.getUserName());
		report=(TextView)findViewById(R.id.report);report.setText("20");
		
		changeLanguage();
		
		rdnofix.setOnClickListener(colorNoFix);
		rderror.setOnClickListener(colorError);
		rdfixcomplete.setOnClickListener(colorFixComplete);
	}  
	
	private OnClickListener colorFixComplete=new OnClickListener() {
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			namecolor="#3DA850";
			Spannable WordtoSpan = new SpannableString(comment.getText().toString());        
			WordtoSpan.setSpan(new ForegroundColorSpan(Color.parseColor(namecolor)), 0, comment.getText().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			comment.setText(WordtoSpan);
		}
	};
	private OnClickListener colorError=new OnClickListener() {
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			namecolor="#B02323";
			Spannable WordtoSpan = new SpannableString(comment.getText().toString());        
			WordtoSpan.setSpan(new ForegroundColorSpan(Color.parseColor(namecolor)), 0, comment.getText().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			comment.setText(WordtoSpan);
		}
	};
	private OnClickListener colorNoFix=new OnClickListener() {
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			namecolor="#B023AB";
			Spannable WordtoSpan = new SpannableString(comment.getText().toString());        
			WordtoSpan.setSpan(new ForegroundColorSpan(Color.parseColor(namecolor)), 0, comment.getText().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			comment.setText(WordtoSpan);
		}
	};
	
	private void changeLanguage(){
		lbtitle=(TextView)findViewById(R.id.lbtitle);
		lbcontenterror=(TextView)findViewById(R.id.lbcontenterror);
		lbcomment=(TextView)findViewById(R.id.lbcomment);
		lblistcommnet=(TextView)findViewById(R.id.lblistcommnet);

		rdnofix=(RadioButton)findViewById(R.id.rdnofix);
		rderror=(RadioButton)findViewById(R.id.rderror);
		rdfixcomplete=(RadioButton)findViewById(R.id.rdfixcomplete);
		
		lbtitle.setText(language.getString("title"));
		lbcontenterror.setText(language.getString("content"));
		lbcomment.setText(language.getString("comment"));
		lblistcommnet.setText(language.getString("l_commentlist"));
		btok.setText(language.getString("ok"));
		btcancel.setText(language.getString("cancel"));
	}
	
	private OnClickListener listenerLogout=new OnClickListener() {
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent i=new Intent(Projectdetail_error_tc.this, LogoutActivity.class);
			i.putExtra("database", getDatabase());
			startActivity(i);
		}
	};
	
	private OnItemClickListener showMenu=new OnItemClickListener() {
		public void onItemClick(AdapterView<?> a, View v, int position, long id) {
			AlertDialog.Builder adb=new AlertDialog.Builder(Projectdetail_error_tc.this);
			
			prcom=(PrComment) listcomment.getItemAtPosition(position);
			
			adb.setTitle(prcom.getCommentview());
			if(emp.getIdemployee().equalsIgnoreCase(prcom.getIdEmployee())){
//				adb.setMessage(language.getString("are-you-sure"));
				adb.setPositiveButton(language.getString("Delete"), new android.content.DialogInterface.OnClickListener(){
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						deleteItemSelected(prcom.getIdComment());
					}});
				adb.setNegativeButton(language.getString("ac_Cancel"), null);
			}else{
//				adb.setMessage(language.getString("ac_KhongTheXoaThongTin"));
				adb.setNegativeButton(language.getString("ac_Cancel"), null);
			}
			adb.show();
		}
	};
	
	private void deleteItemSelected(String comment){
		prdao.deleteItemComment(getDatabase(),comment);
		Projectdetail_error_adapter_tc adapter1 = new Projectdetail_error_adapter_tc(this, loadAllComment());
		listcomment.setAdapter(adapter1);
	}
	
	private List<PrComment> loadAllComment(){
		List<PrComment> item=new ArrayList<PrComment>();
		item=prdao.getLoadAllCommentDAO_tc(getDatabase(),err.getIdTest());
		return item;
	}
	
	private OnClickListener clickListenerCancel=new OnClickListener() {
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			showListenErrorCancel();
		}
	};
	
	public void showListenErrorCancel(){
		Intent i=new Intent(this,Projectdetail_tab_tc.class);
		if(i!=null)
			loadIntent(i);
	}
	
	private OnClickListener clickListenerOK=new OnClickListener() {
		public void onClick(View v) {
			// TODO Auto-generated method stub
			showListError(comment.getText().toString());
		}
	};
	
	private void showListError(String comment){
		prdao.addNewCommentErrorDAO_tc(getDatabase(),err.getIdTest(),emp.getIdemployee(),comment,namecolor);
	
		Intent i=new Intent(this,Projectdetail_error_tc.class);
		if(i!=null)
			loadIntent(i);
		
		Projectdetail_error_adapter_tc adapter1 = new Projectdetail_error_adapter_tc(this, loadAllComment());
		listcomment.setAdapter(adapter1);
	}
	

	private void loadIntent(Intent i){
		i.putExtras(getIntent().getExtras());
		i.putExtra("errorproject", err);
		i.putExtra("project", pro);
		i.putExtra("lang", lang);
		i.putExtra("database", getDatabase());
		i.putExtra("em", emp);
		startActivity(i);
	}
}
